/*
 * Copyright (c) 2001, 2003 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation. Boris Pruessmann -
 * Added support for file types.
 */
package net.sourceforge.perforce.core.internal.api;

import com.google.common.collect.Lists;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IFileType;
import net.sourceforge.perforce.core.api.IOpenedResult;
import net.sourceforge.perforce.core.api.IPerforceServer;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.PlatformObject;

import java.util.List;
import java.util.Map;

/**
 * ClientUser object for the 'p4 opened' command.
 * 
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class Opened extends AbstractPerforceCommand implements IPerforceCommand {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the operation's result. */
  private List<Result> result = Lists.newArrayList();
  /**
   * Stores the perforce server. TODO: Remove
   */
  private IPerforceServer perforceServer;

  // --------------------------------------------------------------- Public
  // Static Factory Methods

  /**
   * Convenience method for creating instances of Opened.
   * 
   * @param perforceServer the perforce server object.
   * @return the new Opened instance.
   */
  public static final Opened newInstance(IPerforceServer perforceServer) {
    MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null);
    return new Opened(status, perforceServer);
  }

  // ------------------------------------------------------------------------
  // Private Constructors

  /**
   * Constructor for Opened.
   * 
   * @param status the MultiStatus instance.
   * @param perforceServer the server.
   */
  private Opened(MultiStatus status, IPerforceServer perforceServer) {
    super(status);
    this.perforceServer = perforceServer;
  }

  // -------------------------------------------------------- Public Methods
  // (IClientUserDelegate)

  @Override
  public void outputStat(Map<String, ?> strDict) {
    Result tempResult = new Result();
    tempResult.setDepotFilename(Helper.safeGetString(strDict, "depotFile")); //$NON-NLS-1$
    tempResult.setClientFilename(Helper.safeGetString(strDict, "clientFile")); //$NON-NLS-1$
    tempResult.setRevision(Helper.safeGetInteger(strDict, "rev")); //$NON-NLS-1$
    tempResult.setAction(Helper.safeGetString(strDict, "action")); //$NON-NLS-1$
    tempResult.setChangelist(Helper.safeGetChangelist(strDict, "change")); //$NON-NLS-1$
    tempResult.setFileType(Helper.safeGetFileType(strDict, "type")); //$NON-NLS-1$
    tempResult.setUserName(Helper.safeGetString(strDict, "user")); //$NON-NLS-1$
    tempResult.setClientName(Helper.safeGetString(strDict, "client")); //$NON-NLS-1$
    tempResult.setPerforceServer(perforceServer);

    if (strDict.containsKey("ourLock")) {
      tempResult.setOurLock(true);
    }

    result.add(tempResult);
  }

  // ----------------------------------------------------------- Public Methods
  // (IPerforceCommand)

  public String getCommand() {
    return "opened"; //$NON-NLS-1$
  }

  @Override
  public IStatus getStatus() {
    return getMultiStatus();
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  /**
   * Returns the result of the operation.
   * 
   * @return array of result objects.
   */
  public IOpenedResult[] getResult() {
    return result.toArray(new IOpenedResult[result.size()]);
  }

  // ----------------------------------------------------------------------
  // Private Nested Classes

  /**
   * Implementation of the IOpenedResult. Stores information provided by the 'p4
   * opened' command.
   * 
   * @see IOpenedResult TODO: Remove PlatformObject dependency.
   */
  private static class Result extends PlatformObject implements IOpenedResult {
    // -----------------------------------------------------------------------
    // Private Variables

    /** Stores the action. */
    private String action;
    /** Stores the changelist number. */
    private Integer changelist;
    /** Stores the client name. */
    private String clientName;
    /** Stores the filename in client syntax. */
    private String clientFilename;
    /** Stores the filename in depot syntax. */
    private String depotFilename;
    /** Stores the revision number. */
    private Integer revision;
    /** Stores the filetype. */
    private IFileType type;
    /** Stores the username. */
    private String userName;
    /** Stores the ourLock value. */
    private boolean ourLock;
    /**
     * Stores the perforce server. TODO: Remove
     */
    private IPerforceServer perforceServer;

    //--------------------------------------------------------------------------
    // Public Methods

    public String getAction() {
      return action;
    }

    public Integer getChangelist() {
      return changelist;
    }

    public String getClientName() {
      return clientName;
    }

    public String getClientFilename() {
      return clientFilename;
    }

    public String getDepotFilename() {
      return depotFilename;
    }

    public Integer getRevision() {
      return revision;
    }

    public IFileType getFileType() {
      return type;
    }

    public String getUserName() {
      return userName;
    }

    public boolean hasOurLock() {
      return ourLock;
    }

    public IPerforceServer getPerforceServer() {
      return perforceServer;
    }

    //--------------------------------------------------------------------------
    // ------- Methods

    /**
     * Sets the action.
     * 
     * @param action The action to set
     */
    void setAction(String action) {
      this.action = action;
    }

    /**
     * Sets the changelist.
     * 
     * @param changelist The changelist to set
     */
    void setChangelist(Integer changelist) {
      this.changelist = changelist;
    }

    /**
     * Sets the clientFilename.
     * 
     * @param clientFilename The clientFilename to set
     */
    void setClientFilename(String clientFilename) {
      this.clientFilename = clientFilename;
    }

    /**
     * Sets the clientName.
     * 
     * @param clientName The clientName to set
     */
    void setClientName(String clientName) {
      this.clientName = clientName;
    }

    /**
     * Sets the depotFilename.
     * 
     * @param depotFilename The depotFilename to set
     */
    void setDepotFilename(String depotFilename) {
      this.depotFilename = depotFilename;
    }

    /**
     * Sets the perforceServer.
     * 
     * @param perforceServer The perforceServer to set
     */
    void setPerforceServer(IPerforceServer perforceServer) {
      this.perforceServer = perforceServer;
    }

    /**
     * Sets the revision.
     * 
     * @param revision The revision to set
     */
    void setRevision(Integer revision) {
      this.revision = revision;
    }

    /**
     * Sets the type.
     * 
     * @param type The type to set
     */
    void setFileType(IFileType type) {
      this.type = type;
    }

    /**
     * Sets the userName.
     * 
     * @param userName The userName to set
     */
    void setUserName(String userName) {
      this.userName = userName;
    }

    /**
     * Sets the ourLock value.
     * 
     * @param newOurLock the new our lock value.
     */
    void setOurLock(boolean newOurLock) {
      ourLock = newOurLock;
    }
  }
}
